package com.ta.gulimall.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ta.gulimall.common.enums.ProductAttrTypeEnums;
import com.ta.gulimall.common.utils.system.PageUtils;
import com.ta.gulimall.common.utils.system.Query;
import com.ta.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.ta.gulimall.product.view.entity.AttrAttrgroupRelationEntity;
import com.ta.gulimall.product.service.AttrAttrgroupRelationService;
import com.ta.gulimall.product.view.vo.AttrVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveAttrAttrGroupRelation(AttrVo attr) {
        if (attr.getAttrType().equals(ProductAttrTypeEnums.BASIC_ATTR.getCode())) {
            AttrAttrgroupRelationEntity attrAttrgroupRelation = BeanUtil.copyProperties(attr, AttrAttrgroupRelationEntity.class);
            this.save(attrAttrgroupRelation);
        }
    }

    @Override
    public void updateAttrAttrgroupRelation(AttrVo attr) {
        if (attr.getAttrType().equals(ProductAttrTypeEnums.MARKET_ATTR.getCode())) {
            return;
        }
        if (ObjectUtil.isNotEmpty(attr.getAttrGroupId())) {
            LambdaQueryWrapper<AttrAttrgroupRelationEntity> queryWrapper = new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
                    .eq(AttrAttrgroupRelationEntity::getAttrId, attr.getAttrId());
            this.update(new AttrAttrgroupRelationEntity().setAttrGroupId(attr.getAttrGroupId()), queryWrapper);
        }
    }

    @Override
    public List<AttrAttrgroupRelationEntity> getAttrAttrgroupRelations(Long attrgroupId) {
        LambdaQueryWrapper<AttrAttrgroupRelationEntity> wrapper = new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
                .eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrgroupId);

        return this.list(wrapper);
    }

    @Override
    public List<AttrAttrgroupRelationEntity> getAttrAttrgroupRelations(List<Long> attrgroupIds) {
        LambdaQueryWrapper<AttrAttrgroupRelationEntity> wrapper = new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
                .in(AttrAttrgroupRelationEntity::getAttrGroupId, attrgroupIds);

        return this.list(wrapper);
    }

    @Override
    public AttrAttrgroupRelationEntity getAttrAttrgroupRelation(Long attrId) {
        LambdaQueryWrapper<AttrAttrgroupRelationEntity> wrapper = new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
                .eq(AttrAttrgroupRelationEntity::getAttrId, attrId);

        return this.getOne(wrapper);
    }

}